package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.support.jaxrs.FastJsonAutoDiscoverable;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 菜单控制层
 * @author ：zengjie
 * @date ：2020/11/16
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping(value = "/findMenu",method = RequestMethod.GET)
    public Result findMenu(String username){
        try {
//            System.out.println(username);
            String jedisList = jedisPool.getResource().get(username);
            List<Menu> menuList =null;
            List<Menu> parse = null;
//           如果jedis里面没有数据，调用服务查询数据
            if (jedisList ==null) {
                //获取一级菜单
                menuList = menuService.findAllMenu(username);
                //获取二级菜单
                if (menuList != null && menuList.size() > 0){
                    for (Menu menu : menuList) {
                        List<Menu> childrenList = menuService.findChildren(menu.getId(), username);
                        menu.setChildren(childrenList);
                    }
                }
                //附带加上
                Menu menu = new Menu();
                menu.setPath("1");
                menu.setName("工作台");
                menu.setIcon("fa-dashboard");
                menuList.add(0, menu);
//          将List转换成字符串 并且存入redis
                jedisList= JSON.toJSONString(menuList);
                jedisPool.getResource().set(username, jedisList);
            }
            parse = (List<Menu>) JSON.parse(jedisList);

            return new Result(true, MessageConstant.GET_MENU_SUCCESS,parse);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    /*
    * 菜单分页查询
    * */
    @RequestMapping(value = "/findPage",method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        try {
            return menuService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
    * 菜单新增
    * */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
        }catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
    }

    /*
    * 编辑菜单
    * */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody Menu menu){
        try {
            menuService.update(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }

    /*
    * 查询角色
    * */
    @RequestMapping(value = "/queryRoles",method = RequestMethod.POST)
    public Result queryRoles(){
        return null;
    }

    /*
    * 查询角色
    * */
    @RequestMapping(value = "/findAllRoles",method = RequestMethod.POST)
    public Result findAllRoles(){
        try {
            List<Map> list = menuService.findAllRoles();
            return new Result(true,MessageConstant.QUERY_ROLES_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLES_FALL);
        }
    }
    /*
    * 删除菜单
    * */
    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    public Result deleteById(Integer id){
        try {
            Integer result = menuService.queryParenMenu(id);
            if (result>0) {
                return new Result(false,MessageConstant.DELETE_MENUANDROLES_FAIL);
            }
            menuService.deleteById(id);

            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }

    @RequestMapping(value = "/findById",method = RequestMethod.POST)
    public Result findById( Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
}

